package com.xerland.tbootstrap.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * Fileame: com.xerland.tbootstrap.utils
 * Desription:
 * Class and methods:
 * <p>
 * Author: Anson Chan
 * Copyright: 2016 Xerland Co. Ltd. All rights reserved.
 * Contact:    anson_chan@xerland.com
 * Deffield    updated: Updated
 */
public class SpfManager {
    public static final String SPF_ENTRANCE = "com.xerland.tbootstrap";

    final static String baseTopic = "/saibao/led";
    final static String baseSubscription = "/saibao/led";
    final static String rtdUpdateSubscription = "/rtd/update";
    final static String powerUpdateSubscription = "/powerstate/update";
    final static String rtdResponseSubscription = "/rtd/response";
    final static String powerResponseSubscription = "/powerstate/response";
    final static String rtdRequestPublish = "/rtd/request";
    final static String powerRequestPublish = "/powerstate/request";


    final static String serverUrlDef = "iot.eclipse.org";
    final static int serverPortDef = 1883;
    final static String peerIdDef = "cafe" ;
    final static String userIdDef = "tea";
    final static String userCipherDef = "sugars";


    private Context context;

    // Mqtt context
//    private String rtdUpdateSub;
//    private String powerUpdateSub;
//    private String rtdResponseSub;
//    private String powerRequestSub;
//    private String rtdRequestPub;
//    private String powerRequestPub;
//    private String hostName;
//    private int hostPort;
//    private String peerId;
//
//    private String userId;
//    private String cipher;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    public SpfManager(Context context) {
        this.context = context;
        sharedPreferences = context.getSharedPreferences(SPF_ENTRANCE, Activity.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public String loadRtdUpdateSub() {
        return sharedPreferences.getString("rtdUpdateSub",rtdUpdateSubscription ) ;
    }

    public String loadPowerUpdateSub() {
        return sharedPreferences.getString("powerUpdateSub",powerUpdateSubscription ) ;
    }

    public String loadRtdResponseSub(){
        return sharedPreferences.getString("powerUpdateSub", powerUpdateSubscription ) ;
    }

    public String loadPowerResponsePub() {
        return sharedPreferences.getString("powerResponseSub", powerResponseSubscription ) ;
    }

    public String loadRtdRequestPub() {
        return sharedPreferences.getString("rtdRequestSub", rtdRequestPublish ) ;
    }

    public String loadPowerRequestPub() {
         return sharedPreferences.getString("powerRequestSub", powerRequestPublish ) ;
    }

    public String loadBaseTopic() {
        return sharedPreferences.getString("baseTopic", baseTopic );
    }

    public String loadServerUrl() {
        return sharedPreferences.getString("serverUrl", serverUrlDef ) ;
    }

    public int loadServerPort() {
        return sharedPreferences.getInt("serverPort", serverPortDef);
    }

    public String loadPeerId() {
        return sharedPreferences.getString("peerId", peerIdDef);
    }

    public String loadUserId() {
        return sharedPreferences.getString("userId", userIdDef);
    }

    public String loadUserCipher(){
        return sharedPreferences.getString("userCipher", userCipherDef);
    }


    public void saveRtdUpdateSub(String value) {
        editor.putString("rtdUpdateSub",value ) ;
        editor.commit();
    }

    public void savePowerUpdateSub(String value) {
        editor.putString("powerUpdateSub",value ) ;
        editor.commit();
    }

    public void saveRtdResponseSub(String value){
        editor.putString("powerUpdateSub", value ) ;
        editor.commit();
    }

    public void savePowerResponsePub(String value) {
        editor.putString("powerResponseSub", value ) ;
        editor.commit();
    }

    public void saveRtdRequestPub(String value) {
        editor.putString("rtdRequestSub", value ) ;
        editor.commit();
    }

    public void savePowerRequestPub(String value) {
        editor.putString("powerRequestSub", value ) ;
        editor.commit();
    }

    public void saveBaseTopic(String value) {
        editor.putString("baseTopic", value );
        editor.commit();
    }

    public void saveServerUrl(String value) {
        editor.putString("serverUrl", value ) ;
        editor.commit();
    }

    public void saveServerPort(int value) {
        editor.putInt("serverPort", value);
        editor.commit();
    }

    public void savePeerId(String value) {
        editor.putString("peerId", value);
        editor.commit();
    }

    public void saveUserId(String value) {
        editor.putString("userId", value);
        editor.commit();
    }

    public void saveUserCipher(String value){
        editor.putString("userCipher", value);
        editor.commit();
    }

}